<?php

namespace App\Admin\Controllers;

use App\Models\Seek;
use App\Models\User;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class SeekController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '影视留言';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Seek());

        $grid->column('id', __('Id'));
        $grid->column('name', __('影视名称'));
        $grid->column('user', __('用户名称'))->display(function($user){
            return $user['nickname'];
        });
        $states = [
            'on'  => ['value' => 1, 'text' => '已处理', 'color' => 'success'],
            'off' => ['value' => 0, 'text' => '未处理', 'color' => 'warning'],
        ];
        $grid->column('phone', __('手机号码'));
        $grid->column('created_at', __('创建时间'));
        $grid->column('updated_at', __('更新时间'));
        $grid->column('status', __('状态'))->switch($states);

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Seek::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('name', __('影视名称'));
        $show->field('user', __('用户名称'))->as(function ($user){
            return $user['nickname'];
        });
        $show->field('phone', __('手机号码'));
        $show->field('status', __('状态'))->using([0=>'未处理',1=>'已处理']);
        $show->field('created_at', __('创建时间'));
        $show->field('updated_at', __('更新时间'));
        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Seek());

        $form->text('name', __('影视名称'))->rules('required');
        $form->select('user_id', __('用户名称'))->options(function ($id){
            $video = User::find($id);
            if ($video) {
                return [$video->id => $video->nickname];
            }
        })->ajax('/api/admin_user_list')->rules('required');
        $form->text('phone',__('手机号码'))->rules('required');
        $states = [
            'on'  => ['value' => 1, 'text' => '已处理', 'color' => 'success'],
            'off' => ['value' => 0, 'text' => '未处理', 'color' => 'warning'],
        ];

        $form->switch('status', __('状态'))->states($states);

        return $form;
    }
}
